fix(java): emit @JsonFormat for optional date/time properties#3049
Merged
Conversation
With --datetime-provider legacy, JavaJacksonRenderer switched on the raw property type kind to decide whether to add @jsonformat. Required date/date-time/time properties have that kind directly, but optional properties are represented as a nullable union, so the switch fell through and the annotation was silently omitted, producing inconsistent serialization between required and optional fields of the same format. Unwrap a nullable union to its non-null member (matching the pattern already used in JavaRenderer.javaType) before switching on the kind. Added test/inputs/schema/optional-date-time.schema (+ .1.json sample) with matching required/optional date, time, and date-time properties, exercised by the existing schema-java-datetime-legacy fixture. Fixes #1593 Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
With
--datetime-provider legacy, the Java (Jackson) renderer attaches@JsonFormat(pattern = ...)todate/date-time/timeproperties thatare required, but silently omits it for the same kind of properties
when they are optional — producing inconsistent serialization/parsing
behavior between the two, even though both hold the same value type.
Repro:
with a schema containing both a required and an optional
date-timeproperty. Before the fix, only the required property got
@JsonFormat;after the fix, both do.
Root cause
JavaJacksonRenderer.annotationsForAccessorswitched directly onp.type.kindto decide which@JsonFormatannotation to emit. Arequired date/date-time/time property has that kind directly on its
type, so the switch matched. An optional property is represented
internally as a nullable union (
date-time | null), sop.type.kindis"union"and none of the cases matched, silently skipping theannotation. This was invisible with the default
java8datetimeprovider because that provider's annotation lists are empty regardless.
Fix
Unwrap a nullable union to its non-null member before switching on the
kind, using the same
nullableFromUnionpattern already used elsewherein the Java renderer (e.g.
JavaRenderer.javaType). Minimal, four-linechange in
packages/quicktype-core/src/language/Java/JavaJacksonRenderer.ts.Test coverage
Added
test/inputs/schema/optional-date-time.schema(+.1.jsonsample) with matching required and optionaldate,time, anddate-timeproperties. This schema is picked up automatically by theexisting
schema-java-datetime-legacyfixture (JavaLanguageWithLegacyDateTimein
test/languages.ts, registered intest/fixtures.ts), and is notexcluded by that language's
skipSchemalist.Verification done locally
npm run buildpasses.optionalDate/requiredDatenow getidentical
@JsonFormatannotations under--datetime-provider legacy.java8provider still emits zero@JsonFormatannotations (unaffected/expected behavior).
other languages (C#, Python, Go, Rust, Swift, Kotlin, TypeScript) to
confirm the new fixture input doesn't break unrelated languages.
fixture (
FIXTURE=schema-java-datetime-legacy script/test) could notbe executed end-to-end locally; CI will run it.
Fixes #1593
🤖 Generated with Claude Code